//
//  AppDelegate.swift
//  DocReaderRP
//
//  Created by  mac on 2025/11/10.
//

import UIKit
import IQKeyboardManager
import FirebaseCore


@main
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?
    var application: UIApplication?
    var aLaunchOptions: [UIApplication.LaunchOptionsKey: Any]?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        
        self.application = application
        self.aLaunchOptions = launchOptions
        
        FirebaseApp.configure()
        
        IQKeyboardManager.shared().isEnabled = true
        IQKeyboardManager.shared().shouldResignOnTouchOutside = true
        
        DR_NativeAdManager.shared.loadConfig()
        initializeWindow()
        
        return true
    }

    private func initializeWindow(){
        
        window = UIWindow(frame: UIScreen.main.bounds)
        self.window?.rootViewController = DR_TabBarVC()
        self.window?.makeKeyAndVisible()
    }
    
    // MARK: - 刷新整个应用（用于语言切换等场景）
    func reloadApplication() {
        // 重新初始化window，刷新整个app
        initializeWindow()
    }
    
    // MARK: - 应用生命周期
    func applicationDidEnterBackground(_ application: UIApplication) {
        // 应用进入后台时，确保UserDefaults同步到磁盘
        UserDefaults.standard.synchronize()
    }
    
    func applicationWillTerminate(_ application: UIApplication) {
        // 应用即将终止时，确保UserDefaults同步到磁盘
        UserDefaults.standard.synchronize()
    }

}

